package com.code.family.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.code.family.entity.pojo.PageParamType;
import com.code.family.entity.pojo.Transactions;
import com.code.family.service.TransactionsService;
import com.code.family.mapper.TransactionsMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class TransactionsServiceImpl extends ServiceImpl<TransactionsMapper, Transactions>
    implements TransactionsService{

    @Resource
    private TransactionsMapper transactionsMapper;

    @Override
    public boolean add(Transactions transaction) {
        // 如果更新条数>0
        if(transactionsMapper.insert(transaction)>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean delete(int id) {
        // 如果更新条数>0
        if (transactionsMapper.deleteById(id) > 0){
            return true;
        }
        return false;
    }

    @Override
    public List<Transactions> selectById(int id) {
        List<Transactions> list = transactionsMapper.selectById(id);
        return list;
    }

    @Override
    public boolean update(Transactions transaction) {
        // 如果更新条数>0
        if (transactionsMapper.updateTranslations(transaction) > 0){
            return true;
        }
        return false;
    }

    // 多表查询
    @Override
    public List<Transactions> find(PageParamType param) {
        // 计算分页的偏移量
        param.setCurrentPage((param.getCurrentPage()-1) * param.getPageSize());
        List<Transactions> transactions = transactionsMapper.find(param);
        log.info("查询所有交易记录：{}", transactions);
        return transactions;
    }
}




